package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.mapper.LikesMapper;
import com.heima.behavior.mapper.ReadMapper;
import com.heima.behavior.mapper.UnLikeMapper;
import com.heima.behavior.service.BehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadCountDto;
import com.heima.model.behavior.dtos.UnLikebehaviorDto;
import com.heima.model.behavior.pojos.LikesConfig;
import com.heima.model.behavior.pojos.ReadConfig;
import com.heima.model.behavior.pojos.UnlikeConfig;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.AdminThreadLoacalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author earl
 * @Data 2024/8/21下午4:20
 * @Version 1.0
 */
@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    LikesMapper likesMapper;

    @Autowired
    UnLikeMapper unLikeMapper;

    @Autowired
    ReadMapper readMapper;

    /**
     * 喜欢和取消喜欢接口
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        Integer userId = AdminThreadLoacalUtil.getId();
        LambdaQueryWrapper<LikesConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LikesConfig::getUserId, userId);
        wrapper.eq(LikesConfig::getArticleId, dto.getArticleId());
        LikesConfig likesConfig = likesMapper.selectOne(wrapper);

        if (likesConfig != null) {
            // 如果LikesConfig存在，则更新operation字段
            // 假设dto中有一个getOperation()方法来获取要更新的operation值
            likesConfig.setOperation(dto.getOperation());
            // 更新数据库中的记录
            likesMapper.updateById(likesConfig);
        } else {
            // 如果LikesConfig不存在，你可能需要执行一些其他操作，比如插入新记录
            // 但根据你的需求，这里我们可能只是记录一个日志或返回一个错误响应
            // 例如：
            // 或者，如果你希望在这种情况下也插入新记录（但只设置operation字段），你可以这样做：
             LikesConfig newConfig = new LikesConfig();
             BeanUtils.copyProperties(dto, newConfig);
             newConfig.setUserId(userId);
             likesMapper.insert(newConfig);
        }

        // 返回结果，这里假设我们总是返回OK，因为即使没有找到LikesConfig，我们也没有抛出异常
        return ResponseResult.okResult(null);
    }

    /**
     * 不喜欢和取消不喜欢接口
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikeBehavior(UnLikebehaviorDto dto) {
        Integer userId = AdminThreadLoacalUtil.getId();
        LambdaQueryWrapper<UnlikeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnlikeConfig::getUserId, userId);
        wrapper.eq(UnlikeConfig::getArticleId, dto.getArticleId());
        UnlikeConfig unlikeConfig = unLikeMapper.selectOne(wrapper);

        if (unlikeConfig != null) {
            // 如果LikesConfig存在，则更新operation字段
            // 假设dto中有一个getOperation()方法来获取要更新的operation值
            unlikeConfig.setType(dto.getType());
            // 更新数据库中的记录
            unLikeMapper.updateById(unlikeConfig);
        } else {

            UnlikeConfig newConfig = new UnlikeConfig();
            BeanUtils.copyProperties(dto, newConfig);
            newConfig.setUserId(userId);
            System.out.println("Before insert: " + newConfig);
            unLikeMapper.insert(newConfig);
            System.out.println("After insert: " + newConfig);

        }
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult readContent(ReadCountDto dto) {
        LambdaQueryWrapper<ReadConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReadConfig::getArticleId,dto.getArticleId());
        Integer id = AdminThreadLoacalUtil.getId();
        wrapper.eq(ReadConfig::getUserId,id);

        ReadConfig readConfig = readMapper.selectOne(wrapper);
        if (readConfig==null){
           readConfig = new ReadConfig();
           BeanUtils.copyProperties(dto,readConfig);
           readConfig.setUserId(id);
           readMapper.insert(readConfig);
           return ResponseResult.okResult(null);
        }
        readConfig.setReadCount(readConfig.getReadCount()+1);
        readMapper.updateById(readConfig);
        return ResponseResult.okResult(null);
    }
}